package com.ruoyi.demo;

import cn.hutool.core.lang.Console;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StreamUtils;
import com.ruoyi.demo.domain.TestDemo;
import com.ruoyi.demo.domain.vo.TestDemoVo;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.function.Function;

/**
 * <简述>
 * <详细描述>
 *
 * @author syf
 * @date 2024年08月13日 10:38
 */
public class TestStreamUtils {

    public static List<TestDemo> getList() {
        List<TestDemo> list = new ArrayList<>();
        TestDemo demo1 = new TestDemo();
        demo1.setTestKey("1");
        demo1.setOrderNum(1);
        list.add(demo1);

        TestDemo demo2 = new TestDemo();
        demo2.setTestKey("2");
        demo2.setOrderNum(2);
        list.add(demo2);

        TestDemo demo3 = new TestDemo();
        demo3.setTestKey("3");
        demo3.setOrderNum(5);

        TestDemo demo4 = new TestDemo();
        demo4.setTestKey("1");
        demo4.setOrderNum(4);

        list.add(demo1);
        list.add(demo2);
        list.add(demo3);
        list.add(demo4);
        return list;
    }




    //过滤
    static void testFilter() {
        List<TestDemo> list = getList();
        List<TestDemo> filter = StreamUtils.filter(list, demo -> "1".equals(demo.getTestKey()));
        for (TestDemo demo : filter) {
            Console.log("demo->{}", demo);
        }
    }

    //
    static void testJoin() {
        List<TestDemo> list = getList();
        String join = StreamUtils.join(list, demo -> demo.getTestKey(), "--");
        Console.log("join->{}", join);
    }

    static void testSort() {
        List<TestDemo> list = getList();
        List<TestDemo> sorted = StreamUtils.sorted(list, (o1, o2) -> o1.getOrderNum() - o2.getOrderNum());
        for (TestDemo demo : sorted) {
            Console.log("sorted->{}", demo);
        }
    }

    //指的属性作为key，返回map
    static void  testToIdentityMap(){
        List<TestDemo> list = getList();
        Map<String, TestDemo> map = StreamUtils.toIdentityMap(list, demo -> demo.getTestKey());
        //Map<String, TestDemo> map1 = StreamUtils.toIdentityMap(list, demo -> demo.getOrderNum().toString());
        for (String key : map.keySet()) {
            Console.log("testToIdentityMap->{}->{}", key, map.get(key));
        }
    }

    //指的属性作为key，value返回类型重新封装，返回map
    static void  toMap(){
        List<TestDemo> list = getList();
        Map<String, Object> map = StreamUtils.toMap(list, new Function<TestDemo, String>() {
                @Override
                public String apply(TestDemo testDemo) {
                    return testDemo.getOrderNum().toString();
                }
            },
            new Function<TestDemo, Object>() {
                @Override
                public Object apply(TestDemo testDemo) {
                    TestDemoVo copy = BeanCopyUtils.copy(testDemo, TestDemoVo.class);
                    return copy;
                }
            }
        );

        for (Object key : map.keySet()) {
            Console.log("toMap->{}->{}", key, map.get(key));
        }
    }

    //分组
    static void  testGroup(){
        List<TestDemo> list = getList();
        Map<String, List<TestDemo>> map = StreamUtils.groupByKey(list, demo -> demo.getTestKey());
        for (String key : map.keySet()) {
            Console.log("testGroup->{}->{}", key, map.get(key));
        }
    }
    //分2组，外层根据testKey ，内层根据：orderNum
    static void  testGroup2(){
        List<TestDemo> list = getList();
        Map<String, Map<Integer, List<TestDemo>>> stringMapMap = StreamUtils.groupBy2Key(list, demo -> demo.getTestKey(), demo -> demo.getOrderNum());
        for (String keyout : stringMapMap.keySet()) {
            Map<Integer, List<TestDemo>> integerListMap = stringMapMap.get(keyout);
            Console.log("外层->{}->{}", keyout, stringMapMap.get(keyout));
            for (Integer keyin : integerListMap.keySet()) {
                Console.log("内层->{}->{}", keyin, integerListMap.get(keyin));
            }
        }
    }

    static void  testToList(){
        List<TestDemo> list = getList();
        //返回单个属性集合
        List<String> strings = StreamUtils.toList(list, demo -> demo.getTestKey());
        Console.log("testToList->{}", strings);

        //转换为vo类
        List<TestDemoVo> testDemoVos = StreamUtils.toList(list, demo -> {
            TestDemoVo copy = BeanCopyUtils.copy(demo, TestDemoVo.class);
            return copy;
        });
        Console.log("testToList->{}", testDemoVos);
    }

    //转换为set （去掉重复数据）
    static void  testToSet(){
        List<TestDemo> list = getList();
        Set<String> strings = StreamUtils.toSet(list, demo -> demo.getTestKey());
        Console.log("testToSet->{}", strings);
    }

    static void  testMerge(){
        List<TestDemo> list = getList();
        //先得到2个，key同类型，value 类型不一样的 map
        Map<String, String> map1 = StreamUtils.toMap(list, demo -> demo.getTestKey(), demo -> demo.getTestKey());
        Map<String, Integer> map2 = StreamUtils.toMap(list, demo -> demo.getOrderNum()+"",demo->demo.getOrderNum());

        //合并
        Map<String, String> merge = StreamUtils.merge(map1, map2, (value1, value2) -> value1 + "--" + value2);
        Console.log("testMerge->{}", merge);
    }

    public static void main(String[] args) {
        //testFilter();
        //testJoin();
        //testSort();
        //testToIdentityMap();
        //toMap();
        //testGroup();
        // testGroup2();
        //testToList();
        //testToSet();
        testMerge();


    }


}
